<?php
namespace app\madmin\model;
use think\Model;

/**
 * 商品列表模型类
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class MallGoodsModel extends Model
{

    protected $name = 'MallGoods';

    /**
     * 数据列表方法
     * @param   Array   $Where  查询条件
     * @param   String  $Field  查询字段
     * @param   Int     $Limit  每页条数
     * @param   Srting  $Order  查询排序
     * @return  Array
     */
    public function mList($Where = [], $Field = '*', $Limit = 15, $Order = 'goods_id DESC'){
        $Count = $this->mCount($Where);

        $Where['goods_del'] = 1;

        return $this->field($Field)->where($Where)->order($Order)->paginate($Limit, $Count, ['query'=>request()->param()]);
    }

    /**
     * 数据统计方法
     * @param   Array   $Where  查询条件
     * @return  Int
     */
    public function mCount($Where = []){
        $Where['goods_del'] = 1;

        return $this->where($Where)->count();
    }

    /**
     * 数据详情方法
     * @param   Array   $Where  查询条件
     * @param   String  $Field  查询字段
     * @return  Array
     */
    public function mInfo($Where = [], $Field = '*'){
        $Where['goods_del'] = 1;

        return $this->field($Field)->where($Where)->find();
    }

    /**
     * 数据详情方法
     * @param   Array   $Where  查询条件
     * @param   String  $Field  查询字段
     * @return  String
     */
    public function mField($Where, $Field){
        $Where['goods_del'] = 1;

        return $this->where($Where)->value($Field);
    }

    /**
     * 数据新增方法
     * @param   Array   $Param  添加内容
     * @return  Array
     */
    public function mAdd($Param){
        $Code = 2;
        $Time = time();

        try{
            $Param['goods_create'] = $Time;

            $Result = $this->allowField(true)->insert($Param);

            $AddID = $this->getLastInsID();

            if($Result){
                $Code = 1;
                $Msgs = lang('XtTsTjCg');
            }else{
                $Code = 2;
                $Msgs = lang('XtTsTjSb');
            }

            addMemberLog(session('AdminID'), 2, lang('SpWpRzXz', ['m'=>session('AdminUser'), 'd'=>$AddID]), $Code, $Msgs);

            return ['code'=>$Code, 'msgs'=>$Msgs, 'urls'=>url('MallGoods/showList')];
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

    /**
     * 数据更新方法
     * @param   Array   $Where  更新条件
     * @param   Array   $Param  更新内容
     * @return  Array
     */
    public function mEdit($Where, $Param){
        $Code = 2;
        $Time = time();

        try{
            $Find = $this->mInfo($Where);

            if(empty($Find)){
                $Msgs = lang('XtCwCxCw');
            }else{
                $Where['goods_del'] = 1;

                $Param['goods_update'] = $Time;

                $Result = $this->allowField(true)->where($Where)->update($Param);

                if($Result){
                    $Code = 1;
                    $Msgs = lang('XtTsGxCg');
                }else{
                    $Msgs = lang('XtTsGxSb');
                }
            }

            addMemberLog(session('AdminID'), 3, lang('SpWpRzGx', ['m'=>session('AdminUser'), 'd'=>$Where['goods_id']]), $Code, $Msgs);

            return ['code'=>$Code, 'msgs'=>$Msgs, 'urls'=>url('MallGoods/showList')];
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

    /**
     * 数据删除方法
     * @param   Array   $Where  更新条件
     * @return  Array
     */
    public function mDel($Where){
        $Code = 2;
        $Time = time();

        try{
            $Find = $this->mInfo($Where);

            if(empty($Find)){
                $Msgs = lang('XtCwCxCw');
            }else{
                $Where['goods_del'] = 1;

                $Param = [];
                $Param['goods_del'] = 2;
                $Param['goods_update'] = $Time;

                $Result = $this->allowField(true)->where($Where)->update($Param);

                if($Result){
                    $Code = 1;
                    $Msgs = lang('XtTsScCs');
                }else{
                    $Msgs = lang('XtTsScSb');
                }
            }

            addMemberLog(session('AdminID'), 4, lang('SpWpRzSc', ['m'=>session('AdminUser'), 'd'=>$Where['goods_id']]), $Code, $Msgs);

            return ['code'=>$Code, 'msgs'=>$Msgs];
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

}